<?php
// +----------------------------------------------------------------------
// | API接口 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------

namespace app\api\controller;
use app\admin\controller\Config;
use app\common\controller\ApiFornt;

//首页
class Index extends ApiFornt {

	public function _initialize() {
		parent::_initialize();
	}
	
	
    /**
      * getbook [获取首页推荐数据]
      *
      * @param type $type  [分类]
      * @param type $limit [条数]
      *
      * @return type
    **/
	public function getbook() {
		$type = input('type')?input('type'):1;
		$channel = input('channel')?input('channel'):1;
		$start = input('start')?input('start'):0;
		$page = input('limit')?input('limit'):10;
		$limit = $start."',".$page."'";		
		
		if($type==5){
			$recommend = db('novel')->where(['status'=>1,'display'=>1,'channel'=>$channel])->cache(true,60)->field('id,title,keywords,cover,intro,allvisit,author,category_id')->order('allvisit DESC')->limit($limit)->select();
		}else{
			$recommend = db('novel')->where([''=>['exp','FIND_IN_SET('.$type.',is_top)'],'status'=>1,'display'=>1,'channel'=>$channel])->field('id,title,keywords,cover,intro,author,keywords,allvisit,goodnum,category_id')->cache(true,60)->page($limit)->select();
		}
		if($recommend){
			foreach ($recommend as $k=>&$v){
				$v['intro'] =strip_tags($v['intro']);
				$v['cover'] = $v['cover']?$v['cover']:BASE_PATH . '/public/novel/images/default.jpg';
				$v['category'] = get_category($v['category_id'],'title');
			}
			$this->ReturnJson(1,'ok',$recommend);
		}else{
			$this->ReturnJson(0,"");
		}
	}

	public function allbooks(){
		$channel = input('channel')?input('channel'):1;
		$fullflag = input('fullflag');
		$top = input('top')?input('top'):0;
		$cate = input('cate');
		$start = input('start')?input('start'):0;
		$page = input('limit')?input('limit'):10;
		$limit = $start."',".$page."'";	
		$order = 'id DESC';

		if($fullflag=="free"){
			//限时免费
			$free = db('limitedfree')->where('enddate >'.time())->order('create_time DESC')->limit(1)->find();
			
			$limitedfree = array();
			if($free){
				$limitedfree = explode(",",$free['articleid']);
				foreach ($limitedfree as $k=>$v){
					$limitedfree[$k] = db('novel')->where(array('status'=>1,'display'=>1,'id'=>$v))->field('id,title,intro,cover,fullflag,author,allvisit,category_id')->find();
				}
			}

			if($limitedfree){
				foreach($limitedfree as $k=>&$v){
					$v['cover'] = $v['cover']?$v['cover']:"/public/novel/images/default.jpg";
					$v['link'] = '/book/info/id/'.$v['id'];
					//$v['category'] = get_category($v['category_id'],'title');
					$v['desc'] = $v['intro'];
				}
				$this->ReturnJson(1,'ok',array('total'=>$total,"data"=>$booklist));
			}else{
				$this->ReturnJson(0,"");
			}
		}else{
	
			//完结
			if(isset($fullflag)&&$fullflag!=null){
				$map['fullflag'] = input('fullflag');
			}
			if(!empty($cate)){
				$map['category_id'] = $cate;
			}
			if(!empty($top)){
				if($top==5){
					$order = 'allvisit DESC';
				}else{
					$map[''] = ['exp','FIND_IN_SET('.$top.',is_top)'];
				}
			}
			$map['status'] = 1;
			$map['display'] = 1;
			
			$booklist = db('novel')->where($map)->field('id,title,intro,cover,fullflag,author,allvisit,category_id')->limit($limit)->order($order)->select();

			$total = db('novel')->where($map)->field('id')->order($order)->count();

			if($booklist){
				foreach($booklist as $k=>&$v){
					$v['cover'] = $v['cover']?$v['cover']:"/public/novel/images/default.jpg";
					$v['link'] = '/book/info/id/'.$v['id'];
					//$v['category'] = get_category($v['category_id'],'title');
					$v['desc'] = $v['intro'];
				}
				$this->ReturnJson(1,'ok',array('total'=>$total,"data"=>$booklist));
			}else{
				$this->ReturnJson(0,"");
			}
		}
	}
	/****获取首页排行数据****/
	public function ranking() {
		$type = input('type')?input('type'):1;
		$channel = input('channel')?input('channel'):1;
		$start = input('start')?input('start'):0;
		$page = input('limit')?input('limit'):10;
		$limit = $start."',".$page."'";
		
		if($type==3){//新作
			$ranking = db('novel')->where(['channel'=>$channel,'status'=>1,'display'=>1])->cache(true,60)->field('id,title,keywords,cover,intro,author')->order('create_time DESC')->limit($limit)->select();
		}elseif($type==4){//完结
			$ranking = db('novel')->where(['channel'=>$channel,'status'=>1,'display'=>1,'fullflag'=>1])->cache(true,60)->field('id,title,keywords,cover,intro,author,allvisit')->limit($limit)->select();
		}elseif($type==5){//畅销
			$ranking = db('novel')->where(['status'=>1,'display'=>1])->cache(true,60)->field('id,title,keywords,cover,intro,allvisit,author')->order('allvisit DESC')->limit($limit)->select();
		}elseif($type==6){//小说精选
			$ranking = db('novel')->where(array('status'=>1,'display'=>1,'channel'=>$channel,''=>['exp','FIND_IN_SET(3,is_top)']))->field('id,title,keywords,cover,intro,allvisit,author')->order('create_time DESC')->cache(true,60)->limit($limit)->select();
		}
		if($ranking){
			$this->ReturnJson(1,'ok',$ranking,0);
		}else{
			$this->ReturnJson(2,"");
		}
		
		
	}

	public function search() {
		$keyword = input('keyword');
		$start = input('start')?input('start'):0;
		$page = input('limit')?input('limit'):10;
		$limit = $start."',".$page."'";	

		$list = db('novel')->where(array('title'=>array('like','%'.$keyword.'%'),'status'=>1,'display'=>1))->field('id,title,intro,author,cover,allvisit,category_id,fullflag')->limit($limit)->select();

		//添加搜索次数
		$info = db("search_info")->where(array('type'=>1,'keyword'=>$keyword))->find();
		if($info){
			db("search_info")->where(array('id'=>$info['id']))->exp('search_num','`search_num`+1')->update();
		}else{
			$search=array(
				'type'=>1,
				'keyword'=>$keyword,
				'search_num'=>1,
				'res_sum'=>count($list),
				'created_at'=>time()
			);
			db("search_info")->insert($search);
		}
		
		if($list){
			foreach($list as $k=>$v){
				$list[$k]['title'] = $v['title'];
				$list[$k]['pic'] = $v['cover']?$v['cover']:"/public/novel/images/default.jpg";
				$list[$k]['link'] = '/book/info/id/'.$v['id'];
				$list[$k]['click'] = $v['allvisit'];
				$list[$k]['cate'] = get_category($v['category_id'],'title');
				$list[$k]['desc'] = $v['intro'];
				$list[$k]['status'] = $v['fullflag']==1?"已完结":"连载中";
			}
			
			echo json_encode(array('total'=>count($list),'data'=>$list));
			exit();
		}
		echo json_encode(array('total'=>0));
		exit();

	}

	
    /**
      * category [获取分类接口]
      *
      * @return type
    **/
	public function getcategory() {
		$sex = input('sex');
		if($sex==1){
			$map['name']='男频';
		}else{
			$map['name']='女频';
		}
		$category = db('category')->where(array('display'=>1,'model_id'=>1))->where($map)->cache(true,60)->field('id,title as name')->select();

		if($category){
			$this->ReturnJson(1,'ok',$category,0);
		}else{
			$this->ReturnJson(2,"");
		}
	}

	public function getfree() {
		//限时免费
		$free = db('limitedfree')->where('enddate >'.time())->order('create_time DESC')->limit(1)->find();
		
		$limitedfree = array();
		if($free){
			$limitedfree = explode(",",$free['articleid']);
			foreach ($limitedfree as $k=>$v){
				$limitedfree[$k] = db('novel')->where(array('status'=>1,'display'=>1,'id'=>$v))->field('id,title,cover,intro,category_id,fullflag,allvisit')->find();
			}
		}

		if($limitedfree){
			foreach ($limitedfree as $k=>&$v){
				$v['startdate'] = date('Y-m-d',$free['startdate']);
				$v['enddate'] = date('Y-m-d',$free['enddate']);
			}
			$this->ReturnJson(1,'ok',$limitedfree,0);
		}else{
			$this->ReturnJson(2,"");
		}
	}
	
    /**
      * url_get_ewm [二维码]
      *
      * @return type
    **/
	public function url_get_ewm() {
		//底部二维码
		$host = explode('.',$_SERVER['HTTP_HOST']);
		if(strpos($host[0],'t') !==false){
			$uid = split_host($host[0]);
			$wxmpid= split_host($host[0],2);
			$wechat = db('wxmp')->where(['uid'=>$uid,'id'=>$wxmpid])->value('wechat');
			if($wechat){
				$QRcode = "http://open.weixin.qq.com/qr/code?username=".trim($wechat);
			}else{
				$QRcode =config('agent_qzgzewm');
			}
		}else{
			$QRcode =config('agent_qzgzewm');
		}
		$this->ReturnJson(1,'ok',$QRcode);
	}
}
